﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoCompleteText
{
    class FSSearch
    {
        
        
        public FSSearch() { }

        List<string> chosung = new List<string>()
        {
            "ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", 
            "ㅃ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ",
            "ㅍ", "ㅎ"
        };
                // 중성
                List<string> jungsung = new List<string>()
        {
            "ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ",
            "ㅗ", "ㅘ", "ㅙ", "ㅚ", "ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ",
            "ㅠ", "ㅡ", "ㅢ", "ㅣ"
        };
                // 종성
                List<string> jongsung = new List<string>()
        {
            "", "ㄱ", "ㄲ", "ㄱㅅ", "ㄴ", "ㄴㅈ", "ㄴㅎ", "ㄷ", 
            "ㄹ", "ㄹㄱ", "ㄹㅁ", "ㄹㅂ", "ㄹㅅ", "ㄹㅌ", "ㄹㅍ", "ㄹㅎ", 
            "ㅁ", "ㅂ", "ㅂㅅ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ",
            "ㅍ", "ㅎ"
        };

        public string StringToJaso(string str)
        {
            int hangul, jong, jung, cho;
            string result = "";
            int i = 0;

            for (i = 0; i < str.Length; i++)
            {
                if (str[i] < 128)    // 영문
                    result += str[i];
                else if (str[i] >= 0xAC00 && str[i] <= 0xD7A3)  // 한글(완성형) : 0xAC00 -> '가', 0xD7A3 -> '힣'
                {
                    hangul = (int)(str[i]) - 0xAC00;

                    jong = hangul % 28;
                    jung = ((hangul - jong) / 28) % 21;
                    cho = ((hangul - jong) / 28) / 21;

                    result += chosung.ElementAt(cho) + jungsung.ElementAt(jung) + jongsung.ElementAt(jong);
                }
                else if (str[i] >= 0x3131 && str[i] <= 0x3163)  // 한글(자소) : 0x3131 -> 'ㄱ', 0x3163 -> 'l'
                {
                    result += str[i];
                }
            }
            return result;
        }

        public String[] Direct(String query)
        {
            try
            {
                char b = query.ToCharArray(0, 1)[0];
                String chosung = null;
                String[] returnQuery = new String[2];
                int first;
                if (b >= 44032)
                {
                    first = (b - 44032) / (21 * 28);
                    switch (first)
                    {
                        case 0:
                        case 1:
                            chosung = "ㄱ";
                            returnQuery[0] = "가";
                            returnQuery[1] = "깋";
                            break;
                        case 2:
                            chosung = "ㄴ";
                            returnQuery[0] = "나";
                            returnQuery[1] = "닣";
                            break;
                        case 3:
                        case 4:
                            chosung = "ㄷ";
                            returnQuery[0] = "다";
                            returnQuery[1] = "딯";
                            break;
                        case 5:
                            chosung = "ㄹ";
                            returnQuery[0] = "라";
                            returnQuery[1] = "맇";
                            break;
                        case 6:
                            chosung = "ㅁ";
                            returnQuery[0] = "마";
                            returnQuery[1] = "밓";
                            break;
                        case 7:
                        case 8:
                            chosung = "ㅂ";
                            returnQuery[0] = "바";
                            returnQuery[1] = "빟";
                            break;
                        case 9:
                        case 10:
                            chosung = "ㅅ";
                            returnQuery[0] = "사";
                            returnQuery[1] = "싷";
                            break;
                        case 11:
                            chosung = "ㅇ";
                            returnQuery[0] = "아";
                            returnQuery[1] = "잏";
                            break;
                        case 12:
                        case 13:
                            chosung = "ㅈ";
                            returnQuery[0] = "자";
                            returnQuery[1] = "짛";
                            break;
                        case 14:
                            chosung = "ㅊ";
                            returnQuery[0] = "차";
                            returnQuery[1] = "칳";
                            break;
                        case 15:
                            chosung = "ㅋ";
                            returnQuery[0] = "카";
                            returnQuery[1] = "킿";
                            break;
                        case 16:
                            chosung = "ㅌ";
                            returnQuery[0] = "타";
                            returnQuery[1] = "팋";
                            break;
                        case 17:
                            chosung = "ㅍ";
                            returnQuery[0] = "파";
                            returnQuery[1] = "핗";
                            break;
                        case 18:
                            chosung = "ㅎ";
                            returnQuery[0] = "하";
                            returnQuery[1] = "힣";
                            break;

                    }
                }
                else
                {
                    first = (b - 12593);
                    switch (first)
                    {
                        case 0:
                            chosung = "ㄱ";
                            returnQuery[0] = "가";
                            returnQuery[1] = "깋";
                            break;

                        case 3:
                            chosung = "ㄴ";
                            returnQuery[0] = "나";
                            returnQuery[1] = "닣";
                            break;
                        case 6:
                            chosung = "ㄷ";
                            returnQuery[0] = "다";
                            returnQuery[1] = "딯";
                            break;
                        case 8:
                            chosung = "ㄹ";
                            returnQuery[0] = "라";
                            returnQuery[1] = "맇";
                            break;
                        case 16:
                            chosung = "ㅁ";
                            returnQuery[0] = "마";
                            returnQuery[1] = "밓";
                            break;
                        case 17:
                            chosung = "ㅂ";
                            returnQuery[0] = "바";
                            returnQuery[1] = "빟";
                            break;
                        case 20:
                            chosung = "ㅅ";
                            returnQuery[0] = "사";
                            returnQuery[1] = "싷";
                            break;
                        case 22:
                            chosung = "ㅇ";
                            returnQuery[0] = "아";
                            returnQuery[1] = "잏";
                            break;
                        case 23:
                            chosung = "ㅈ";
                            returnQuery[0] = "자";
                            returnQuery[1] = "짛";
                            break;
                        case 25:
                            chosung = "ㅊ";
                            returnQuery[0] = "차";
                            returnQuery[1] = "칳";
                            break;
                        case 26:
                            chosung = "ㅋ";
                            returnQuery[0] = "카";
                            returnQuery[1] = "킿";
                            break;
                        case 27:
                            chosung = "ㅌ";
                            returnQuery[0] = "타";
                            returnQuery[1] = "팋";
                            break;
                        case 28:
                            chosung = "ㅍ";
                            returnQuery[0] = "파";
                            returnQuery[1] = "핗";
                            break;
                        case 29:
                            chosung = "ㅎ";
                            returnQuery[0] = "하";
                            returnQuery[1] = "힣";
                            break;



                    }
                }



                return returnQuery;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
